<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="warning"
                    size="small"
                    
                    v-if="permission.cityworkBacktimeout_bkh"
                    icon="el-icon-unlock"
                    @click="handleBkh">设置为不考核工单
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          icon="el-icon-chat-line-square"
          size="small"
          @click.stop="handleWorkDetail(scope.row,scope.index)">工单详情
        </el-button>
        <!-- <el-button
          type="text"
          icon="el-icon-chat-line-square"
          size="small"
          @click.stop="handleSheDetail(scope.row,scope.index)"
          v-if="scope.row.status !=0">审核详情
        </el-button> -->
        <el-button
          type="text"
          icon="el-icon-chat-line-square"
          size="small"
          @click.stop="handleShe(scope.row,scope.index)"
          v-if="scope.row.status ==0">审核
        </el-button>
       </template>
    </avue-crud>
    <!-- 查看工单详情 -->
    <el-dialog title="工单信息"
                   append-to-body
                   :visible.sync="viewReplayBoxShow"
                   width="50%">
    <el-form ref="ViewReplayForm" :rules="rules" :model="viewReplayForm" label-width="180px" size="mini">
     <el-card shadow="hover">
        <el-row>
          <el-col :span="24">
            <el-form-item label="工单号：" >
            {{ viewReplayForm.workId  }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="接收时间：">
                {{ viewReplayForm.receiptTime }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="问题描述：">
               {{ viewReplayForm.problemDescription }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="工单附件：">
              空
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="转办意见：">
               {{ viewReplayForm.administrativeOpinions }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="回复时限：">
                {{ viewReplayForm.timeLimit }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="联系人：">
               {{ viewReplayForm.contact }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="联系电话：">
               {{ viewReplayForm.contactPhone }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="地址：">
               {{ viewReplayForm.address }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="紧急程度：">
               {{ viewReplayForm.urgencyDegree }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="是否保密：">
                {{ viewReplayForm.confidentiality }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="是否回复：">
              {{ viewReplayForm.replyFlag }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="回复备注：">
              {{ viewReplayForm.replyRemark }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="办理轨迹：">
               {{ viewReplayForm.handlerTrack }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="转办人：">
              {{ viewReplayForm.administrativePerson }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="受理类型：">
              {{ viewReplayForm.accessType }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="受理员：">
              {{ viewReplayForm.acceptorName }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="受理员工号：">
              {{ viewReplayForm.acceptorNumber }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="录入时间：">
              {{ viewReplayForm.inputTime }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="信息来源：">
              {{ viewReplayForm.messageSource }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="分类：">
              {{ viewReplayForm.classify }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="工单类型：">
              {{ viewReplayForm.workOrderType }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="操作记录：" style="white-space: pre-wrap">
              {{ historyDetail }}
            </el-form-item>
          </el-col>
        </el-row>
         <el-row>
              <el-form-item class="align-text:center">
                <el-button @click="handleViewReplayCancel()">关闭</el-button>
              </el-form-item>
         </el-row>
      </el-card>
     </el-form>
    </el-dialog>
    <!-- 工单审核 -->
    <el-dialog title="工单审核"
                   append-to-body
                   :visible.sync="sheBoxShow"
                   width="50%">
    <el-form ref="SheForm" :rules="rules" :model="sheForm" label-width="180px" size="mini">
     <el-card shadow="hover">
        <el-row>
          <el-col :span="24" >
            <el-form-item label="id：" v-show="false">
            {{ sheForm.id  }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="工单号：" >
            {{ sheForm.workId  }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="回退部门：">
                {{ sheForm.fallbackToDept }}
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="回退原因：">
               {{ sheForm.fallbackReason }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="发起部门：">
               {{ sheForm.fromDeptName }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="审核：" prop="status">
               <el-radio-group v-model="sheForm.status">
                <el-radio label="1">通过</el-radio>
                <el-radio label="2">驳回</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
            <el-col :span="24" >
            <el-form-item label="回退理由"  prop="auditOpinion">
              <el-input  type="textarea" v-model="sheForm.auditOpinion"/>
            </el-form-item>
          </el-col>
        </el-row>
         <el-row>
              <el-form-item class="align-text:center">
                 <el-button type="primary" @click="handleSheSave('SheForm')">保存</el-button>
                <el-button @click="handleSheCancel('SheForm')">关闭</el-button>
              </el-form-item>
         </el-row>
      </el-card>
     </el-form>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,bkh,sheSave} from "@/api/govsvc/cityworkBacktimeout";
  import {getRejectDetail } from "@/api/govsvc/citywork";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
          //查看回复信息form
        viewReplayForm: {

        },
        //审核form
        sheForm: {
        },
        //Form表单校验
        rules: {
          status: [
            {required: true, message: '请选择审核状态', trigger: 'blur' }
          ],
        },
        historyDetail: [],
        sheBoxShow: false,//审核窗口显示
        viewReplayBoxShow: false,//查看回复信息窗口显示
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
           indexLabel:'序号',
          height: 'auto',
          calcHeight: 30,
          dialogWidth: 850,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          addBtn: false,
          editBtn: false,
          delBtn: true,
          selection: true,
          excelBtn: false,
          dialogClickModal: false,
          column: [
            {
              label: "主键id",
              prop: "id",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "工单号",
              prop: "workId",
              type: "input",
              span: 24,
              search: true,
            },
            {
              label: "回退部门",
              prop: "fallbackToDept",
              type: "input",
              span: 24,
            },
            {
              label: "回退理由",
              prop: "fallbackReason",
              type: "textarea",
              overHidden: true,
              span: 24,
            },
            {
              label: "发起部门",
              prop: "fromDept",
              type: "select",
              dicUrl: "/api/smart-system/dept/getDeptList",
              props: {
                label: "title",
                value: "key"
              },
              span: 24,
            },
            {
              label: "类型",
              prop: "applicationType",
              type: "input",
              dicData: [
                {
                  label: "超时回退",
                  value: "1"
                },
                
              ],
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
           
            {
              label: "是否考核",
              prop: "sfkh",
              type: "select",
               dicData: [
                {
                  label: "不考核",
                  value: "1"
                },
                {
                  label: "考核",
                  value: "0"
                },
                
              ],
              span: 24,
            },
            
            {
              label: "审核状态",
              prop: "status",
              type: "select",
              span: 24,
              dicData: [
                {
                  label: "未审核",
                  value: 0
                },
                {
                  label: "审核通过",
                  value: 1
                },
                {
                  label: "审核不通过",
                  value: 2
                },
                
              ],
              search: true,
            },
             {
              label: "审核意见",
              prop: "auditOpinion",
              type: "input",
              overHidden: true,
              span: 24,
              hide: false,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.cityworkBacktimeout_add, false),
          viewBtn: this.vaildData(this.permission.cityworkBacktimeout_view, false),
          delBtn: this.vaildData(this.permission.cityworkBacktimeout_delete, false),
          editBtn: this.vaildData(this.permission.cityworkBacktimeout_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
        //设置为不考核工单
      handleBkh(){
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择的工单设置为不考核工单吗？", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
             return bkh(this.ids);
           })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      //审核窗口显示
      handleShe(row){
          getDetail(row.id).then(res =>{
            this.sheForm = res.data.data;
            this.sheBoxShow = true;
          });

      },
       //保存审核信息
      handleSheSave(form,done,loading) {
          this.$refs[form].validate((valid) => {
            if (!valid){
              console.log('error submit!!');
              return false;
            }else{
              const id = this.sheForm.id;
              const status = this.sheForm.status;
              const auditOpinion = this.sheForm.auditOpinion;
              console.log(id);
                  sheSave(id,status,auditOpinion).then(() => {
                    this.$message({
                      type: "success",
                      message: "保存审核成功!"
                    });
                    this.sheBoxShow = false;
                    this.onLoad(this.page);
                  }, error => {
                    loading();
                    window.console.log(error);
                  });
            }
          });
      },//驳回信息保存完成
       //查看回复详情
      handleWorkDetail(row){
          console.log(row.workId);
          getRejectDetail(row.workId).then(res => {
          console.log(res.data.data);
            this.viewReplayForm = res.data.data.citywork;
            this.historyDetail = res.data.data.historyDetail;
            this.viewReplayBoxShow = true;
            
          });
      },
    
       //查看弹出层关闭
      handleViewReplayCancel(){
          this.viewReplayBoxShow = false;
      },
       //审核弹出层关闭
      handleSheCancel(FormName){
        this.sheBoxShow = false;
        this.$refs[FormName].resetFields();
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
